﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.DAL
{
    /// <summary>
    /// The repository for friendships data manipulation.
    /// </summary>
    public class FriendshipsRepository : BaseRepository<Friendship>, IFriendshipsRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FriendshipsRepository"/> class.
        /// </summary>
        /// <param name="dataServiceContext">The data service context.</param>
        public FriendshipsRepository(IDataServiceContext<Friendship> dataServiceContext)
            :base(dataServiceContext)
        {

        }

        /// <summary>
        /// Gets the friendship between two users.
        /// </summary>
        /// <param name="userID1">The ID of the user 1.</param>
        /// <param name="userID2">The ID of the user 2.</param>
        /// <returns>The firendhsip.</returns>
        public Friendship Get(string userID1, string userID2)
        {
            return (from friendship in Entities
                    where (friendship.User == userID1 && friendship.Friend == userID2)
                    select friendship).FirstOrDefault();
        }

        /// <summary>
        /// Determines if two users are friends.
        /// </summary>
        /// <param name="userID1">The ID of the user 1.</param>
        /// <param name="userID2">The ID of the user 2.</param>
        /// <returns>True if the two users are friends, otherwise false.</returns>
        public bool AreFriends(string userID1, string userID2)
        {
            return Get(userID1,userID2) != null;
        }

        /// <summary>
        /// Deletes the friendship between two users.
        /// </summary>
        /// <param name="userID1">The ID of the user 1.</param>
        /// <param name="userID2">The ID of the user 2.</param>
        public void Delete(string userID1, string userID2)
        {
            var friendship = Get(userID1,userID2);
            if (friendship != null) Delete(friendship);

            friendship = Get(userID2, userID1);
            if (friendship != null) Delete(friendship);
        }

        /// <summary>
        /// Adds a friendship between two users.
        /// </summary>
        /// <param name="userID1">The ID of the user 1.</param>
        /// <param name="userID2">The ID of the user 2.</param>
        public void Add(string userID1, string userID2)
        {
            var friendship = new Friendship(userID1, userID2);
            Add(friendship);
            friendship = new Friendship(userID2, userID1);
            Add(friendship);
        }


        /// <summary>
        /// Gets the friendships by user.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        public List<Friendship> GetFriendshipsByUser(string userID)
        {
            var friends = (from f in Entities
                           where f.User == userID
                           select f).ToList();

            return friends;
        }

        /// <summary>
        /// Deletes all user friend ships.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        public void DeleteAllUserFriendShips(string userID)
        {
            var friends = GetFriendshipsByUser(userID);

            friends.ForEach(f => Delete(f));
        }
    }
}
